
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using ST.Core;
using ST.Data;
using ST.ModelVM;
using ST.ModelVM.ProtocolDefine;
using ST.ModelVM.ProtocolSimHost;
using ST.ModelVM.ProtocolSimLog;
using ST.Simulator.Tasks;


namespace ST.Simulator.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProtocolSimLogController : ControllerBase
    {
         
        IRepository<Data.ProtocolSimLog> _IRepository ;

        
       
        public ProtocolSimLogController(IRepository<Data.ProtocolSimLog> repository1 )
        {
           
            _IRepository = repository1;

         
        }   
        
        [HttpPost("Page")]
        public PageResult<ProtocolSimLog> Page(ProtocolSimLogQM qm){
            var q = _IRepository.GetAll();
            if (qm.ProtocolSimId != 0)
            {
                q=q.Where(a=>a.ProtocolSimId == qm.ProtocolSimId);  
            }
            if(qm.ProtocolSimHostId != 0)
            {
                q=q.Where(a=>a.ProtocolSimHostId == qm.ProtocolSimHostId);
            }

            if (qm.Dtb.HasValue)
            {
                q = q.Where(a => a.Dt >= qm.Dtb.Value);
            }
            if (qm.Dte.HasValue)
            {
                q = q.Where(a => a.Dt <= qm.Dte.Value);
            }
            if (!string.IsNullOrEmpty(qm.Key))
            {
                q = q.Where(a => a.Memo.Contains(qm.Key)||a.Data.Contains(qm.Key) );
            }

            var r=new PageResult<ProtocolSimLog>(); 
            r.Total= q.Count();
            r.Data=q
                .OrderByDescending(a=>a.Id)
                .Skip( (qm.Page-1)*qm.PageSize)
                .Take(qm.PageSize)
                .ToList();
            return r;
        }

         

     

    }
}